/**
 * Project Name:kany-utils
 * File Name:ImageManager.java
 * Package Name:me.kany.utils.image.awt.manager
 * Date:2016-1-7上午11:47:57
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
 */

package me.kany.utils.image.awt.manager;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import me.kany.utils.image.awt.crop.CropImage;

/**
 * ClassName:ImageManager <br/>
 * Function: SWT文件管理类. <br/>
 * Reason: 注意该类主要使用在AWT上. <br/>
 * Date: 2016-1-7 上午11:47:57 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
public class ImageManager {
	private static final String IMAGE_URL_PREFIX = "/com/gauss/swingtest/qq/images/";
	public static final Insets ZERO_INSETS = new Insets(0, 0, 0, 0);
	public static final Font FONT_12_BOLD = new Font("Calibri", 1, 12);
	public static final Color TAB_BOTTOM_LINE_COLOR = new Color(167, 173, 175);
	public static final Color DEFAULT_TEXT_COLOR = new Color(37, 81, 54);

	public static String getImageURL(String imageName) {
		return IMAGE_URL_PREFIX + imageName;
	}

	public static Image getImageWithImgName(String imageName) {
		try {
			return new ImageIcon(ClassLoader.class.getResource(getImageURL(imageName))).getImage();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * getImageWithImgURL:根据图片的URL地址获取当前项目中的图片. <br/>
	 *
	 * @author Jason.Wang
	 * @param imageURL
	 * @return
	 * @since JDK 1.7
	 */
	public static Image getImageWithImgURL(String imageURL) {
		try {
			return new ImageIcon(ClassLoader.class.getResource(imageURL)).getImage();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * getIconWithName:通过指定图片的名称获取图标. <br/>
	 *
	 * @author Jason.Wang
	 * @param iconName
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getIconWithName(String iconName) {
		return new ImageIcon(ClassLoader.class.getResource(getImageURL(iconName)));
	}

	/**
	 * 
	 * getIconWithURL:通过指定的路径获取图片标签. <br/>
	 *
	 * @author Jason.Wang
	 * @param iconURL
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getIconWithURL(String iconURL) {
		return new ImageIcon(ClassLoader.class.getResource(iconURL));
	}

	/**
	 * 
	 * createTexturePaint:根据图片的URL创建一个含有图片背景的文本. <br/>
	 *
	 * @author Jason.Wang
	 * @param imageURL
	 * @return
	 * @since JDK 1.7
	 */
	public static TexturePaint createTexturePaint(String imageURL) {
		return createTexturePaint(getImageWithImgURL(imageURL));
	}

	/**
	 * 
	 * createTexturePaintWithImage:使用图片大小创建一个文本框. <br/>
	 *
	 * @author Jason.Wang
	 * @param image
	 * @return
	 * @since JDK 1.7
	 */
	public static TexturePaint createTexturePaintWithImage(Image image) {
		System.out.println(image.getHeight(null));
		return createTexturePaint(image);
	}

	/**
	 * 
	 * createTexturePaint:创建包含图片的文本背景. <br/>
	 *
	 * @author Jason.Wang
	 * @param image
	 * @return
	 * @since JDK 1.7
	 */
	public static TexturePaint createTexturePaint(Image image) {
		int imageWidth;
		try {
			imageWidth = image.getWidth(null);
			int imageHeight = image.getHeight(null);
			BufferedImage bi = new BufferedImage(imageWidth, imageHeight, 2);
			Graphics2D g2d = bi.createGraphics();
			g2d.drawImage(image, 0, 0, null);
			g2d.dispose();
			return new TexturePaint(bi, new Rectangle(0, 0, imageWidth, imageHeight));
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 
	 * createTransparentButton:创建一个透明的按钮. <br/>
	 *
	 * @author Jason.Wang
	 * @param icon
	 * @param roverIcon
	 * @param pressedIcon
	 * @return
	 * @since JDK 1.7
	 */
	public static JButton createTransparentButton(ImageIcon icon, ImageIcon roverIcon, ImageIcon pressedIcon) {
		JButton button = new JButton();
		button.setCursor(new Cursor(12));
		button.setBorder(null);
		button.setMargin(null);
		button.setOpaque(false);
		button.setIcon(icon);
		button.setRolloverEnabled(true);
		button.setRolloverIcon(roverIcon);
		button.setPressedIcon(pressedIcon);
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.setRequestFocusEnabled(false);
		return button;
	}
	
	/**
	 * 
	 * getImage:通过图片的路径从jar文件中获取图片. <br/>
	 *
	 * @author Jason.Wang
	 * @param imagePath
	 * @return
	 * @since JDK 1.7
	 */
	public static Image getImage(String imagePath) {
		try {
			return new ImageIcon(ClassLoader.class.getResource(imagePath)).getImage();
		} catch (NullPointerException e) {
		}
		return null;
	}

	/**
	 * 
	 * getImageIcon:通过图片的路径获取图标. <br/>
	 *
	 * @author Jason.Wang
	 * @param imagePath
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon getImageIcon(String imagePath) {
		try {
			return new ImageIcon(ClassLoader.class.getResource(imagePath));
		} catch (NullPointerException e) {
		}
		return null;
	}

	/**
	 * 
	 * createIcon:在内存中创建图标. <br/>
	 *
	 * @author Jason.Wang
	 * @param image
	 * @param startx
	 * @param starty
	 * @param width
	 * @param height
	 * @return
	 * @since JDK 1.7
	 */
	public static ImageIcon createIcon(Image image, int startx, int starty, int width, int height) {
		CropImage ci = new CropImage(image, startx, starty, width, height);
		return new ImageIcon(ci.getImage());
	}

	/**
	 * 
	 * OpenUri:打开链接的操作. <br/>
	 *
	 * @author Jason.Wang
	 * @param uriStr
	 * @since JDK 1.7
	 */
	public static void OpenUri(String uriStr) {
		URI uri;
		try {
			uri = new URI(uriStr);
			Desktop desktop = Desktop.getDesktop();
			desktop.browse(uri);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
